package com.social.socialservice.dal;


import com.social.socialservice.idal.INotifyDAL;
import com.social.socialservice.model.NotifyInfo;
import com.social.socialservice.unit.Pager;
import com.social.socialservice.unit.PagerData;
import com.social.socialservice.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息通知实现类
 */
@Service
public class NotifyDAL {
    @Autowired
    INotifyDAL iNotifyDAL;

    /**
     * 查询消息分页
     * 通过当前用户id进行查询
     */
    private List<NotifyInfo> getNotifyMapperByCurrentUserId(Pager pager) {
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1) * pager.getPageSize();
        pager.setStartIndex(startIndex);
        return iNotifyDAL.getNotifyMapperByCurrentUserId(pager);
    }

    /**
     * 查询消息分页的数据总数
     */
    private Integer getNotifyMapperCountsByCurrentUserId(Pager pager) {
        return iNotifyDAL.getNotifyMapperCountsByCurrentUserId(pager);
    }

    /**
     * 查询消息分页
     * 计算分页
     */
    private Integer getNotifyMapperCountsByCurrentUserIdPageTotal(Pager pager) {
        Integer userPostTotalCounts = getNotifyMapperCountsByCurrentUserId(pager);
        Integer pageTotal = 1;
        //数据总条数除以pageSize，如果有余数也不能整除，页数+1
        //可以整除，页数=数据总条/pageSize
        pageTotal = userPostTotalCounts / pager.getPageSize();
        if (userPostTotalCounts % pager.getPageSize() > 0) {
            pageTotal = pageTotal + 1;
        }
        return pageTotal;
    }

    /**
     * 查询消息分页
     * 数据装载
     */
    public Response getNotifyMapperDataByCurrentUserId(Pager pager) {
        //调用封装的好方法进行数据装载
        PagerData pagerData = new PagerData();
        pagerData.setData(getNotifyMapperByCurrentUserId(pager));
        pagerData.setPageTotal(getNotifyMapperCountsByCurrentUserIdPageTotal(pager));
        //嵌入更新已读操作
        updateSelfNotifyByCurrentUserId(pager);
        return new Response(200, "加载数据成功", pagerData);
    }

    /**
     * 如果调用读取了查询消息的分页方法
     * 则修改未读消息为已读，即is_readed=1
     */
    private boolean updateSelfNotifyByCurrentUserId(Pager pager) {
        if (iNotifyDAL.updateSelfNotifyByCurrentUserId(pager) > 0) {
            //标记为已读
            return true;
        }
        return false;
    }

    /**
     * 添加消息通知
     * 1.判断当前回复、转发、点赞操作用户是自己不做操作
     * 2.如果消息已经存在过一条，则不操作，避免重复消息
     * 3.回复、点赞、转发、关注四个操作字段要进行非空判断，如果为空默认值为0
     */
    private boolean addNotifyToUser(NotifyInfo notifyInfo) {
        return iNotifyDAL.addNotifyToUser(notifyInfo) > 0 ? true : false;
    }

    /**
     * 判断消息是否已经存在
     */
    private boolean testNotifyIsExist(NotifyInfo notifyInfo) {
        return iNotifyDAL.testNotifyIsExist(notifyInfo) > 0 ? true : false;
    }

    /**
     * 添加用户消息
     */
    public boolean addNotify(NotifyInfo notifyInfo) {
        //1.判断操作用户不能是自身
        if (notifyInfo.getOpr_user_id() == notifyInfo.getCurrent_user_id()) {
            return false;
        }
        //2.避免关注消息重复
        if(notifyInfo.getIs_foyou()!=null){
            if (notifyInfo.getIs_foyou() > 0) {
                if (testNotifyIsExist(notifyInfo)) {
                    return false;
                }
            }
        }
        //添加消息
        if (addNotifyToUser(notifyInfo)) {
            //添加消息成功
            return true;
        } else {
            //添加消息失败
            return false;
        }
    }

    /**
     * 检索未读的通知消息数量
     */
    public Response getNotifyNotRead(NotifyInfo notifyInfo) {
        return new Response(200, "查询消息数", iNotifyDAL.getNotifyNotRead(notifyInfo));
    }

    /**
     * 消息提示中提及@用户的消息数据
     */
    private List<NotifyInfo> getNotifyMapperATCurrentUserId(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1) * pager.getPageSize();
        pager.setStartIndex(startIndex);
        return iNotifyDAL.getNotifyMapperATCurrentUserId(pager);
    }

    /**
     * 提及消息总数
     */
    private Integer getNotifyMapperCountsATCurrentUserId(Pager pager){
        return iNotifyDAL.getNotifyMapperCountsATCurrentUserId(pager);
    }

    /**
     * 提及消息总数
     * 计算分页
     */
    private Integer getNotifyMapperCountsATCurrentUserIdPageTotal(Pager pager) {
        Integer userPostTotalCounts = getNotifyMapperCountsATCurrentUserId(pager);
        Integer pageTotal = 1;
        //数据总条数除以pageSize，如果有余数也不能整除，页数+1
        //可以整除，页数=数据总条/pageSize
        pageTotal = userPostTotalCounts / pager.getPageSize();
        if (userPostTotalCounts % pager.getPageSize() > 0) {
            pageTotal = pageTotal + 1;
        }
        return pageTotal;
    }

    /**
     * 提及消息
     * 数据装载
     */
    public Response getNotifyMapperDataATCurrentUserId(Pager pager) {
        //调用封装的好方法进行数据装载
        PagerData pagerData = new PagerData();
        pagerData.setData(getNotifyMapperATCurrentUserId(pager));
        pagerData.setPageTotal(getNotifyMapperCountsATCurrentUserIdPageTotal(pager));
        return new Response(200, "加载数据成功", pagerData);
    }

    /**
     * 互动消息指的是点赞和关注
     *
     */
    private List<NotifyInfo> getNotifyMapperHDCurrentUserId(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1) * pager.getPageSize();
        pager.setStartIndex(startIndex);
        return iNotifyDAL.getNotifyMapperHDCurrentUserId(pager);
    }

    /**
     * 互动消息指的是点赞和关注
     *  消息总数
     */
    private Integer getNotifyMapperCountsHDCurrentUserId(Pager pager){
        return iNotifyDAL.getNotifyMapperCountsHDCurrentUserId(pager);
    }

    /**
     * 互动消息指的是点赞和关注
     * 计算分页
     */
    private Integer getNotifyMapperCountsHDCurrentUserIdPageTotal(Pager pager) {
        Integer userPostTotalCounts = getNotifyMapperCountsHDCurrentUserId(pager);
        Integer pageTotal = 1;
        //数据总条数除以pageSize，如果有余数也不能整除，页数+1
        //可以整除，页数=数据总条/pageSize
        pageTotal = userPostTotalCounts / pager.getPageSize();
        if (userPostTotalCounts % pager.getPageSize() > 0) {
            pageTotal = pageTotal + 1;
        }
        return pageTotal;
    }

    /**
     * 互动消息指的是点赞和关注
     * 数据装载
     */
    public Response getNotifyMapperDataHDCurrentUserId(Pager pager) {
        //调用封装的好方法进行数据装载
        PagerData pagerData = new PagerData();
        pagerData.setData(getNotifyMapperHDCurrentUserId(pager));
        pagerData.setPageTotal(getNotifyMapperCountsHDCurrentUserIdPageTotal(pager));
        return new Response(200, "加载数据成功", pagerData);
    }
}
